package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _89岛屿数量
 * @description
 * @date 2023/08/06 10:01
 * <p>
 * ******************************************************
 */
public class _89岛屿数量 {

    public int numIslands(char[][] grid) {
        int res = 0;
        int m = grid.length, n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') {
                    res++;
                    flood(grid, i, j);
                }
            }
        }
        return res;
    }

    int[][] helper = new int[][]{{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

    private void flood(char[][] grid, int i, int j) {
        int m = grid.length, n = grid[0].length;
        if (i < 0 || i >= m || j < 0 || j >= n) {
            return;
        }
        if (grid[i][j] == '1') {
            grid[i][j] = 0;
        } else {
            return;
        }
        for (int[] item : helper) {
            flood(grid, i + item[0], j + item[1]);
        }
    }
}
